package com.ciaojian.worker.controller;

import cn.hutool.core.collection.CollUtil;
import com.ciaojian.core.constant.RedisConstant;
import com.ciaojian.core.enums.business.OrderStatusEnum;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.util.LogUtil;
import com.ciaojian.core.util.RedisUtil;
import com.ciaojian.core.util.Result;
import com.ciaojian.miniapp.model.response.SubMessageSendResponse;
import com.ciaojian.worker.service.OrderServiceImpl;
import com.ciaojian.worker.service.SubMessageServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author Atlakyn
 */
@Api(tags = "订阅消息")
@RestController
@RequestMapping("/sub-message")
public class SubMessageController {
    @Resource
    private SubMessageServiceImpl subMessageService;
    @Resource
    private OrderServiceImpl orderService;

    @ApiOperation("发型师提醒用户付款")
    @PostMapping("/remind-user-payment")
    public Result remindUserPayment(@ApiParam(value = "订单Id") Integer orderId) {
        Assert.notNull(orderId, "请选择订单");
        Order order = new Order().selectById(orderId);
        Assert.notNull(order, "订单不存在");
        Assert.isTrue(order.getStatus().equals(OrderStatusEnum.servicing) || order.getStatus().equals(OrderStatusEnum.wait_pay), "订单已完成");
        Assert.isNull(RedisUtil.getKey(RedisConstant.REMIND_USER_PAYMENT + order.getUserId()), "已提醒过该用户，请10分钟之后再提醒");

        try {
            Result result = subMessageService.remindUserPayment(order);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.writeBusinessLog("发型师提醒用户付款 JsonProcessingException 异常,异常信息:{}", e.getMessage());
            return Result.error("系统异常");
        }
    }

    @ApiOperation("发型师叫号")
    @PostMapping("/call-user")
    public Result callUser(Integer orderId) {
        Assert.notNull(orderId, "请选择订单");
        Order order = new Order().selectById(orderId);
        Assert.notNull(order, "订单不存在");
        Assert.isTrue(order.getStatus().equals(OrderStatusEnum.waiting), "不是待叫号订单");
        Assert.isNull(RedisUtil.getKey(RedisConstant.CALL_USER + order.getUserId()), "已提醒过该用户，请10分钟之后再提醒");

        try {
            Result result = subMessageService.callUser(order);
            return Result.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.writeBusinessLog("发型师叫号 JsonProcessingException 异常,异常信息:{}", e.getMessage());
            return Result.error("系统异常");
        }
    }


    @ApiOperation("排队提醒")
    @PostMapping("/rank-message")
    public Result rankMessage(@RequestParam Integer workerId) {
        // 给后面排队的人发送通知
        List<Order> orderList = orderService.getBaseMapper().getRankNext(workerId);
        if (CollUtil.isNotEmpty(orderList)) {
            try {
                if (orderList.size() == 1) {
                    subMessageService.remindRank(orderList.get(0), null);
                } else {
                    subMessageService.remindRank(orderList.get(0), orderList.get(1));
                }
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.writeBusinessErrorLog("完成订单 Json 转换异常,异常信息:{%s}", e.getMessage());
            }
        }
        return Result.ok();
    }
}
